/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buttonOptions;

import constants.GameControllerServerConstants;
import Helper.ButtonSizeTextFieldValidationListener;
import Helper.ErrorHandler;
import Helper.NumberTextFieldValidationListener;
import controllerButtons.RoundControllerButton;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.colorchooser.DefaultColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author luc0
 */
public class RoundButtonOptionFrame extends JFrame {


    private RoundControllerButton controllerButton;
    private JPanel optionPanel;
    private JButton saveButton;
    private JLabel nameLabel;
    private JTextField nameTextField;
    private JLabel buttonToPressLabel;
    private JLabel sizeHeightLabel;
    private JTextField sizeHeightTextField;
    private JColorChooser colorChooser;
//    private JLabel repeatCheckBoxLabel;
//    private JCheckBox repeatCheckBox;
//    private JSlider repeatCountSlider;
//    private JLabel repeatCounterSliederLabel;
    private boolean repeatsPresses;
    private JComboBox buttonToPressComboBox;

    public RoundButtonOptionFrame(MouseEvent evt, JLayeredPane controllerPanel) {
        this.controllerButton = (RoundControllerButton) evt.getSource();

        optionPanel = new JPanel();


        this.setSize(600, 500);
        this.setLocation(evt.getLocationOnScreen().x, evt.getLocationOnScreen().y);
        optionPanel.setSize(this.getSize());

        //Option Panel
        optionPanel.setLayout(new GridBagLayout());
        final GridBagConstraints c = new GridBagConstraints();

        //Save Button
        saveButton = new JButton("Save");
        saveButton.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                onSaveButtonClicked(e);
            }

            public void mousePressed(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseReleased(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseEntered(MouseEvent e) {
                //  throw new UnsupportedOperationException("Not supported yet.");
            }

            public void mouseExited(MouseEvent e) {
                // throw new UnsupportedOperationException("Not supported yet.");
            }
        });
       
        //Name
        nameLabel = new JLabel("Button Name:");
        nameTextField = new JTextField(controllerButton.getLabel());

        //Button to press
        buttonToPressLabel = new JLabel("Button to press:");
        buttonToPressComboBox = new JComboBox(GameControllerServerConstants.AVAILABLE_KEYS);
        buttonToPressComboBox.setSelectedItem(controllerButton.getButtonToPress());

        //Size Height
        sizeHeightLabel = new JLabel("Size:");
        sizeHeightTextField = new JTextField(String.valueOf(controllerButton.getHeight()));
       // sizeHeightTextField.addFocusListener(new NumberTextFieldValidationListener(this,"height"));
        sizeHeightTextField.addFocusListener(new ButtonSizeTextFieldValidationListener(this,"height",25,220));

        //Color Chooser
        colorChooser = new JColorChooser();
        colorChooser.setPreviewPanel(new JPanel());
        colorChooser.setSelectionModel(new DefaultColorSelectionModel());
        colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                Color newColor = colorChooser.getColor();
                controllerButton.setColor(newColor);
                controllerButton.repaint();
            }
        });


        repeatsPresses = controllerButton.isRepeatsPresses();

//        //Press repeat CheckBox
//        repeatCheckBoxLabel = new JLabel("Multiple button presses by holding:");
//        repeatCheckBox = new JCheckBox();
//        repeatCheckBox.setSelected(controllerButton.isRepeatsPresses());
//        repeatCheckBox.addItemListener(
//                new ItemListener() {
//
//                    public void itemStateChanged(ItemEvent e) {
//
//                        if ((e.getStateChange() == ItemEvent.SELECTED)) {
//                            repeatCountSlider.setEnabled(true);
//                            repeatsPresses = true;
//                            repeatCounterSliederLabel.setEnabled(true);
//                        } else {
//                            repeatCountSlider.setEnabled(false);
//                            repeatsPresses = false;
//                            repeatCounterSliederLabel.setEnabled(false);
//                        }
//
//
//                    }
//                });
//
//        //Repeat Slider
//        repeatCountSlider = new JSlider(5, 1000);
//        repeatCountSlider.setValue(controllerButton.getTimeBetweePresses());
//        repeatCountSlider.setEnabled(controllerButton.isRepeatsPresses());
//        repeatCountSlider.addChangeListener(new ChangeListener() {
//
//            public void stateChanged(ChangeEvent e) {
//                repeatCounterSliederLabel.setText(String.valueOf(repeatCountSlider.getValue()));
//            }
//        });
//
//        //Repeat slider Label
//        repeatCounterSliederLabel = new JLabel(String.valueOf(repeatCountSlider.getValue()));
//        repeatCounterSliederLabel.setEnabled(controllerButton.isRepeatsPresses());

        //Row 1
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        optionPanel.add(nameLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        optionPanel.add(nameTextField, c);



        //Row 2
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        optionPanel.add(buttonToPressLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        optionPanel.add(buttonToPressComboBox, c);


        //Row 3
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        optionPanel.add(sizeHeightLabel, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 2;
        optionPanel.add(sizeHeightTextField, c);


//        //Row 4
//        c.fill = GridBagConstraints.HORIZONTAL;
//        c.gridx = 0;
//        c.gridy = 3;
//        optionPanel.add(repeatCheckBoxLabel, c);
//
//        c.fill = GridBagConstraints.HORIZONTAL;
//        c.gridx = 1;
//        c.gridy = 3;
//        optionPanel.add(repeatCheckBox, c);
//
//        //Row 5
//        c.fill = GridBagConstraints.HORIZONTAL;
//        c.gridx = 0;
//        c.gridy = 4;
//        optionPanel.add(repeatCountSlider, c);
//
//        c.fill = GridBagConstraints.HORIZONTAL;
//        c.gridx = 1;
//        c.gridy = 4;
//        optionPanel.add(repeatCounterSliederLabel, c);


        //Row 6
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 5;
        optionPanel.add(colorChooser, c);


        //Row 7
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        optionPanel.add(saveButton, c);


        this.add(optionPanel);

    }

    private void onSaveButtonClicked(MouseEvent evt) {
        if(!NumberTextFieldValidationListener.isNumberOfTextFieldValid(sizeHeightTextField.getText(),25,220)){
                ErrorHandler.invalidHeight(this);
                return;
            }
        GameControllerServerConstants.isOptionInUse = false;
        controllerButton.setLabel(nameTextField.getText());
        controllerButton.setButtonToPress((String) buttonToPressComboBox.getSelectedItem());
        setSizeOfButton(controllerButton, Integer.parseInt(sizeHeightTextField.getText()));
        controllerButton.setRepeatsPresses(repeatsPresses);
         dispose();
    }

     private void setSizeOfButton(RoundControllerButton button,int height){
        button.setSize(height,height);
        int lx = button.getLocation().x;
        int ly = button.getLocation().y;

        if(lx+height>= 480){
            int pixelsToBorder = 480-lx-5;

            int pixelsHidden = height - pixelsToBorder;
            lx = lx-pixelsHidden;
        }
        if(ly+height>=320){
            int pixelsToBorder = 320-ly-5;

            int pixelsHidden = height-pixelsToBorder;
            ly = ly-pixelsHidden;
        }
         button.setLocation(lx, ly);

     }

}
